package com.qian.service.ddj.serviceImpl;

import com.qian.api.ddj.model.FaildOrderQueryModel;
import com.qian.api.ddj.model.PayFailed;
import com.qian.api.ddj.service.IPayFailedService;
import com.qian.service.ddj.dao.PayFaildRepository;
import com.qian.service.ddj.dao.UserRepository;
import com.qian.service.ddj.entity.DdjPayFailed;
import com.qian.service.ddj.entity.DdjUser;
import com.yaowk.api.common.PageReq;
import com.yaowk.api.common.PageResp;
import com.yaowk.service.common.BaseRepository;
import com.yaowk.service.common.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.*;
import java.util.Date;


@Service
public class PayFailedServiceImpl extends BaseServiceImpl<PayFailed, DdjPayFailed, Integer> implements IPayFailedService {
  @Autowired
  private UserRepository userDao;
  @Autowired
  private PayFaildRepository dao;


    @Override
    public PageResp<PayFailed> findAll(final FaildOrderQueryModel model, PageReq pageReq) {
            PageRequest pageRequest = new PageRequest(pageReq.getPageNumber() - 1, pageReq.getPageSize(), new Sort(Sort.Direction.DESC, "creatTime"));
        Page<DdjPayFailed> page = dao.findAll(new Specification<DdjPayFailed>() {
                @Override
                public Predicate toPredicate(Root<DdjPayFailed> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                    Path<Date> timePath = root.get("creatTime");
                    Predicate condition = criteriaBuilder.isNotNull(root.<Integer>get("id"));
                    // 用户比较
                    if (isNotEmpty(model.getUserId()))
                        condition = criteriaBuilder.and(condition, criteriaBuilder.equal(root.<Integer>get("ddjUserId"), model.getUserId()));
                    if (isNotEmpty(model.getOutOrderNo()))
                        condition = criteriaBuilder.and(condition,criteriaBuilder.equal(root.<String>get("outOrderNo"),model.getOutOrderNo()));

                    // 日期比较
                    if (isNotEmpty(model.getIndexDate()) && isNotEmpty(model.getEndDate()))
                        condition = criteriaBuilder.and(condition, criteriaBuilder.between(timePath, model.getIndexDate(), model.getEndDate()));
                    else if (isNotEmpty(model.getIndexDate()))
                        condition = criteriaBuilder.and(condition, criteriaBuilder.greaterThanOrEqualTo(timePath, model.getIndexDate()));
                    else if (isNotEmpty(model.getEndDate()))
                        condition = criteriaBuilder.and(condition, criteriaBuilder.lessThanOrEqualTo(timePath, model.getEndDate()));

                    return condition;
                }
            }, pageRequest);

        PageResp<PayFailed> pageResp = page2pageResp(page);

            return pageResp;

    }

    protected BaseRepository getDao() {
        return dao;
    }

    @Override
    protected DdjPayFailed model2Entity(PayFailed model) {
        DdjPayFailed ddjPayFaild = null;
        if (isNotEmpty(model)){
            ddjPayFaild = new DdjPayFailed();
            ddjPayFaild.setContent(model.getContent());
            ddjPayFaild.setCreateTime(model.getCreateTime());
            ddjPayFaild.setOutOrderNo(model.getOutOrderNo());
            ddjPayFaild.setParams(model.getParams());
            ddjPayFaild.setReason(model.getReason());
            if (isNotEmpty(model.getUserId())){
                DdjUser ddjUser = new DdjUser();
                ddjUser.setId(model.getUserId());
                ddjPayFaild.setUser(ddjUser);
            }
        }
        return ddjPayFaild;
    }

    @Override
    protected PayFailed entity2model(DdjPayFailed entity) {
        PayFailed payFaild = null;
        if (isNotEmpty(entity)){
            payFaild = new PayFailed();
            payFaild.setContent(entity.getContent());
            payFaild.setCreateTime(entity.getCreateTime());
            payFaild.setOutOrderNo(entity.getOutOrderNo());
            payFaild.setParams(entity.getParams());
            payFaild.setReason(entity.getReason());
            payFaild.setUsername(entity.getUser().getUsername());
        }
        return payFaild;
    }
}
